//
//  ListNolibCell.m
//  OCDemo
//
//  Created by BH on 15/10/14.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "ListNolibCell.h"
#import "GlobalDefine.h"
#import "MLLinkLabel.h"
#import <Masonry/Masonry.h>

@interface ListNolibCell ()

@property(nonatomic,strong)MLLinkLabel *linkLab;

@end

@implementation ListNolibCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        tapGesture.delegate = self;
        
        [self.contentView addGestureRecognizer:tapGesture];
        
        [self.contentView addSubview:self.linkLab];
    }
    return self;
}

-(void)tapAction
{
    SHOW_SIMPLE_TIPS(@"tapped");
}

#pragma mark - getter
-(MLLinkLabel *)linkLab
{
    if (!_linkLab) {
        _linkLab = [MLLinkLabel new];
        _linkLab = [MLLinkLabel new];
        _linkLab.textColor = [UIColor whiteColor];
        _linkLab.backgroundColor = [UIColor colorWithRed:0.137 green:0.780 blue:0.118 alpha:1.000];
        _linkLab.font = [UIFont systemFontOfSize:16.0f];
        _linkLab.numberOfLines = 0;
        _linkLab.textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        _linkLab.lineHeightMultiple = 1.1f;
        
        _linkLab.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};
        _linkLab.activeLinkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor],NSBackgroundColorAttributeName:kDefaultActiveLinkBackgroundColorForMLLinkLabel};
        
        [_linkLab setDidLongPressLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
            SHOW_SIMPLE_TIPS(TIPS);
        }];
        
        [_linkLab setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
            SHOW_SIMPLE_TIPS(TIPS);
        }];
    }
    
    return _linkLab;
}

#pragma mark - gesture delegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //如果此手势在外层的外层view的话，不方便直接定位到label的话，可以使用这种方式。
    CGPoint location = [touch locationInView:gestureRecognizer.view];
    UIView *view = [gestureRecognizer.view hitTest:location withEvent:nil];
    if ([view isKindOfClass:[MLLinkLabel class]]) {
        if ([(MLLinkLabel*)view linkAtPoint:[touch locationInView:view]]) {
            return NO;
        }
    }
    
    return YES;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.linkLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(5);
        make.width.equalTo(@(self.contentView.frame.size.width-10*2));
        make.height.equalTo(@(self.contentView.frame.size.height-5*2));
    }];
}

#pragma mark - height
static MLLinkLabel *kProtypeLabel(){

    static MLLinkLabel *_protypeLab = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _protypeLab = [MLLinkLabel new];
        _protypeLab.font = [UIFont systemFontOfSize:16.0f];
        _protypeLab.numberOfLines = 0;
        _protypeLab.textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        _protypeLab.lineHeightMultiple = 1.1f;
    });
    
    return _protypeLab;
}

+(CGFloat)heightForExpressionText:(NSAttributedString *)expressionText width:(CGFloat)width
{
    //nib里左右边距是10
    width -= 10*2;
    
    MLLinkLabel *label = kProtypeLabel();
    label.attributedText = expressionText;
    
    return [label preferredSizeWithMaxWidth:width].height + 5.0*2;//上下间距
}

@end
